package com.localization;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.Date;

public class FxMessage implements Serializable {

    // 消息ID服务器创建对象后自动生成
    private String msgId;
    // 执行指令
    private int cmd;
    private String fromId;
    private String mac;
    private String ieee;
    private String port;
    private String deviceId;
    private String deviceName;
    private String status;
    private Object extra;
    private JSONArray devices;
    private JSONArray gateways;
    private String time;
    private String power;
    private String rssi;
    private static long serial = 0;

    private static final long serialVersionUID = 6102678755781010665L;

    synchronized static long getSerial() {
        return (System.currentTimeMillis() >> 10) + serial++;
    }

    public FxMessage() {
        msgId = getSerial() + "";
        time = new Date().getTime() + "";
    }

    public FxMessage(String str) {
        try {
            JSONObject jsonMsg = new JSONObject(str);
            if (jsonMsg.has("msgId")) {
                this.setMsgId(jsonMsg.getString("msgId"));
            }
            if (jsonMsg.has("cmd")) {
                this.setCmd(jsonMsg.getInt("cmd"));
            }
            if (jsonMsg.has("fromId")) {
                this.setFromId(jsonMsg.getString("fromId"));
            }
            if (jsonMsg.has("mac")) {
                this.setMac(jsonMsg.getString("mac"));
            }
            if (jsonMsg.has("ieee")) {
                this.setIeee(jsonMsg.getString("ieee"));
            }
            if (jsonMsg.has("port")) {
                this.setPort(jsonMsg.getString("port"));
            }
            if (jsonMsg.has("deviceId")) {
                this.setDeviceId(jsonMsg.getString("deviceId"));
            }
            if (jsonMsg.has("deviceName")) {
                this.setDeviceName(jsonMsg.getString("deviceName"));
            }
            if (jsonMsg.has("status")) {
                this.setStatus(jsonMsg.getString("status"));
            }
            if (jsonMsg.has("devices")) {
                this.setDevices(jsonMsg.getJSONArray("devices"));
            }
            if (jsonMsg.has("gateways")) {
                this.setGateways(jsonMsg.getJSONArray("gateways"));
            }
            if (jsonMsg.has("power")) {
                this.setPower(jsonMsg.getString("power"));
            }
            if (jsonMsg.has("rssi")) {
                this.setRssi(jsonMsg.getString("rssi"));
            }
            if (jsonMsg.has("extra")) {
                this.setExtra(jsonMsg.get("extra"));
            }
        } catch (Exception e) {
        }
    }

    public FxMessage(JSONObject jsonMsg) {
        try {
            if (jsonMsg.has("msgId")) {
                this.setMsgId(jsonMsg.getString("msgId"));
            }
            if (jsonMsg.has("cmd")) {
                this.setCmd(jsonMsg.getInt("cmd"));
            }
            if (jsonMsg.has("fromId")) {
                this.setFromId(jsonMsg.getString("fromId"));
            }
            if (jsonMsg.has("mac")) {
                this.setMac(jsonMsg.getString("mac"));
            }
            if (jsonMsg.has("ieee")) {
                this.setIeee(jsonMsg.getString("ieee"));
            }
            if (jsonMsg.has("port")) {
                this.setPort(jsonMsg.getString("port"));
            }
            if (jsonMsg.has("deviceId")) {
                this.setDeviceId(jsonMsg.getString("deviceId"));
            }
            if (jsonMsg.has("deviceName")) {
                this.setDeviceName(jsonMsg.getString("deviceName"));
            }
            if (jsonMsg.has("status")) {
                this.setStatus(jsonMsg.getString("status"));
            }
            if (jsonMsg.has("devices")) {
                this.setDevices(jsonMsg.getJSONArray("devices"));
            }
            if (jsonMsg.has("gateways")) {
                this.setGateways(jsonMsg.getJSONArray("gateways"));
            }
            if (jsonMsg.has("power")) {
                this.setPower(jsonMsg.getString("power"));
            }
            if (jsonMsg.has("rssi")) {
                this.setRssi(jsonMsg.getString("rssi"));
            }
            if (jsonMsg.has("extra")) {
                this.setExtra(jsonMsg.get("extra"));
            }
        } catch (Exception e) {
        }
    }

    public JSONObject toJson() {
        JSONObject jsonMsg = new JSONObject();
        try {
            jsonMsg.put("msgId", this.getMsgId());
            jsonMsg.put("cmd", this.getCmd());
            jsonMsg.put("fromId", this.getFromId());
            jsonMsg.put("mac", this.getMac());
            jsonMsg.put("ieee", this.getIeee());
            jsonMsg.put("port", this.getPort());
            jsonMsg.put("deviceId", this.getDeviceId());
            jsonMsg.put("deviceName", this.getDeviceName());
            jsonMsg.put("status", this.getStatus());
            jsonMsg.put("devices", this.getDevices());
            jsonMsg.put("gateways", this.getGateways());
            jsonMsg.put("power", this.getPower());
            jsonMsg.put("rssi", this.getRssi());
            jsonMsg.put("extra", this.getExtra());
            jsonMsg.put("time", this.getTime());
        } catch (JSONException e) {
        }
        return jsonMsg;
    }

    public String toString() {
        return this.toJson().toString();
    }

    public int getCmd() {
        return cmd;
    }

    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    public Object getExtra() {
        return extra;
    }

    public void setExtra(Object extra) {
        this.extra = extra;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public String getFromId() {
        return fromId;
    }

    public void setFromId(String fromId) {
        this.fromId = fromId;
    }

    public String getIeee() {
        return ieee;
    }

    public void setIeee(String ieee) {
        this.ieee = ieee;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public JSONArray getDevices() {
        return devices;
    }

    public void setDevices(JSONArray devices) {
        this.devices = devices;
    }

    public JSONArray getGateways() {
        return gateways;
    }

    public void setGateways(JSONArray gateways) {
        this.gateways = gateways;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public String getPower() {
        return power;
    }

    public void setPower(String power) {
        this.power = power;
    }

    public String getRssi() {
        return rssi;
    }

    public void setRssi(String rssi) {
        this.rssi = rssi;
    }

}
